import traceback

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from autoTest_ecshop.utils.log_util import logger

class BasePage():
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 20)

    def find_element(self, locator):
        try:
            logger.info(f"定位元素{locator}")
            element = self.wait.until(EC.visibility_of_element_located(locator))
            logger.info(f"元素{locator}定位成功")
            return element
        except:
            logger.info(f"元素定位失败，失败原因如下：\n{traceback.format_exc()}")
            raise

    def find_elements(self, locator):
        try:
            logger.info(f"定位元素{locator}")
            element = self.wait.until(EC.visibility_of_all_elements_located(locator))
            logger.info(f"元素{locator}定位成功")
            return element
        except:
            logger.error(f"元素定位失败，失败原因如下：\n{traceback.format_exc()}")
            raise

    def input_data(self, locator, value):
        inputBox = self.find_element(locator)
        inputBox.clear()
        logger.info(f"输入的数据是：\n{value}")
        inputBox.send_keys(value)

    def click_btn(self, locator):
        btn = self.find_element(locator)
        btn.click()
        logger.info(f"点击了按钮：{locator}")

    def get_text(self, locator):
        logger.info(f"获取元素{locator}的文本信息")
        element = self.find_element(locator)
        logger.info(f"文本信息为：{element.text}")
        return element.text


    def click_btn1(self, locator):
        btn = self.find_element(locator)
        btn.click()
        logger.info(f"点击了购买诺基亚E66：{locator}")




